package network.link;

import java.io.IOException;
import java.io.OutputStream;

public class FrameOutputStream {
  
  private final OutputStream out_stream_;
  
  /**
   * Constructs a framed output stream
   * @requires stream not null
   * @param stream Output Stream
   */
  public FrameOutputStream(OutputStream stream) {
    assert (stream != null);
    out_stream_ = stream;
  }
  
  /**
   * Writes a sequence of bytes as one message
   * @requires sequence not null
   * @param sequence A sequence of bytes to be sent as a package.
   * @throws IOException iff i/o fails
   */
  public void write(byte[] sequence) throws IOException {
    assert (sequence != null);
    out_stream_.write(Frame.marshall(sequence));
  }
  
  /**
   * Flushes the buffers
   * @throws IOException 
   */
  public void flush() throws IOException {
    out_stream_.flush();
  }
  
  /**
   * Closes this stream
   */
  public void close() {
    try {
      out_stream_.close();
    } catch (IOException e) {
      System.out.println("Failed to close");
    }
  }
  
}
